package org.come.control;

import java.util.List;

import org.come.Frame.BoothBoxJframe;
import org.come.Frame.TransJframe;
import org.come.Jpanel.TestPetJpanel;
import org.come.action.FromServerAction;
import org.come.bean.GoodTrans;
import org.come.bean.PetResultBean;
import org.come.entity.RoleSummoning;
import org.come.model.InternalForm;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;

import com.tool.Stall.Commodity;
import com.tool.Stall.Stall;
import com.tool.Stall.StallBean;
import com.tool.role.RoleData;

public class PetControl implements FromServerAction {

	@Override
	public void controlMessFromServer(String mes, String type) {
		// 获取的召唤兽信息
		PetResultBean petBean = GsonUtil.getGsonUtil().getgson().fromJson(mes, PetResultBean.class);
		// 将召唤兽信息存储在静态中
		UserMessUntil.setPetListTable(qc(petBean.getRoleSummonings()));
		TestPetJpanel.showListModel(UserMessUntil.getPetListTable(), RoleData.getRoleData().getLoginResult().getSummoning_id());
        
		UserMessUntil.setChosePetMes(null);

	}

	// **清除上架的召唤兽
	public static List<RoleSummoning> qc(List<RoleSummoning> roleSummonings) {
		InternalForm form=FormsManagement.getInternalForm2(16);
    	if (form!=null) {
    		Stall stall=((BoothBoxJframe) form.getFrame()).getBoothboxjpanel().getStall2();
    		if (stall != null && (stall.getState() == StallBean.PREPARE || stall.getState() == StallBean.NO)) {
    			Commodity[] pets = stall.getPets();
    			for (int i = 0; i < pets.length; i++) {
    				RoleSummoning pet = pets[i] != null ? pets[i].getPet() : null;
    				if (pet != null) {
    					for (int j = roleSummonings.size() - 1; j >= 0; j--) {
    						if (pet.getSid().compareTo(roleSummonings.get(j).getSid()) == 0) {
    							roleSummonings.remove(j);
    							break;
    						}
    					}
    				}
    			}
    		}
    	}
    	form=FormsManagement.getInternalForm2(14);
		if (form != null) {
			GoodTrans goodTrans = ((TransJframe) form.getFrame()).getTransJpanel().getGoodTrans();
			if (goodTrans != null && goodTrans.getPets() != null) {
				for (int i = 0; i < goodTrans.getPets().size(); i++) {
					RoleSummoning pet = goodTrans.getPets().get(i);
					for (int j = roleSummonings.size() - 1; j >= 0; j--) {
						if (pet.getSid().compareTo(roleSummonings.get(j).getSid()) == 0) {
							roleSummonings.remove(j);
							break;
						}
					}
				}
			}
		}	
		return roleSummonings;
	}
}
